<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属小区：" prop="plotId">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区">
          <el-option
            v-for="(item, index) in xiaoquData"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分组名称：" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入分组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item prop="createTime" label="创建时间：">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:equipment:add']"
          >新增分组</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['equipment:equipment:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="pilinagHandleCilcik"
          v-hasPermi="['equipment:equipment:remove']"
          >批量删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['equipment:equipment:export']">导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="equipmentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="selectFlag"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        show-overflow-tooltip
        label="小区名称"
        align="center"
        prop="plotName"
      />
      <el-table-column
        show-overflow-tooltip
        label="分组名称"
        align="center"
        prop="groupName"
      />
      <el-table-column
        show-overflow-tooltip
        label="创建人"
        align="center"
        prop="createName"
      />
      <el-table-column
        show-overflow-tooltip
        label="创建时间"
        align="center"
        prop="createTime"
      />
      <el-table-column
        show-overflow-tooltip
        label="描述"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['equipment:equipment:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:equipment:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:equipment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备分组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属小区" prop="plotId" style="margin-left: 30px">
          <el-select
            :disabled="form.id"
            v-model="form.plotId"
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in xiaoquDataAdd"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="分组名称"
          prop="groupName"
          style="margin-left: 30px"
        >
          <el-input v-model="form.groupName" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="描述" prop="remark" style="margin-left: 30px">
          <el-input
            type="textarea"
            v-model="form.remark"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="查看分组"
      width="70%"
      :visible.sync="groupData.show"
      append-to-body
    >
      <el-form
        ref="form2"
        :model="form"
        label-position="left"
        label-width="100px"
      >
        <div style="display: flex; flex-wrap: wrap">
          <el-form-item label="所属小区" style="margin-right: 20%">
            <el-input disabled v-model="groupData.row.plotName" />
          </el-form-item>
          <el-form-item label="分组名称">
            <el-input
              disabled
              v-model="groupData.row.groupName"
              placeholder="请输入分组名称"
            />
          </el-form-item>
        </div>
      </el-form>
      <div class="group-bottom-list">
        <h4 class="name">设备列表</h4>
        <el-table v-loading="groupData.loading" :data="groupData.list" :border="false">
          <el-table-column
            show-overflow-tooltip
            label="设备名称"
            align="center"
            prop="equipmentName"
          />
          <!-- <el-table-column show-overflow-tooltip label="设备种类" align="center" prop="equipmentGroupName" /> -->
          <el-table-column
            show-overflow-tooltip
            label="设备种类"
            align="center"
            prop="equipmentTypeName"
          />
          <el-table-column
            show-overflow-tooltip
            label="设备编号"
            align="center"
            prop="equipmentCode"
          />
          <!-- <el-table-column show-overflow-tooltip label="所属小区" align="center" prop="protocolName" /> -->
          <el-table-column
            show-overflow-tooltip
            label="所属小区"
            align="center"
            prop="lotName"
          />
          <el-table-column
            show-overflow-tooltip
            label="所属楼栋"
            align="center"
            prop="buildName"
          />
          <el-table-column
            show-overflow-tooltip
            label="所属单元"
            align="center"
            prop="unitName"
          />
          <!-- <el-table-column show-overflow-tooltip label="房号" align="center" prop="householdName" /> -->
          <el-table-column
            show-overflow-tooltip
            label="房号"
            align="center"
            prop="houseNumber"
          />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleRemove(scope.row)"
                v-hasPermi="['householdClassify:householdClassify:remove']"
                >移除分组</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="myPagination">
          <pagination
            v-show="groupData.total > 0"
            :total="groupData.total"
            :page.sync="groupData.pageNum"
            :limit.sync="groupData.pageSize"
            @pagination="getGroupEquipmentList"
          />
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="groupCancel">取 消</el-button>
        <el-button type="primary" @click="groupSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipmentGroup,
  getEquipmentGroup,
  delEquipmentGroup,
  addEquipmentGroup,
  updateEquipmentGroup,
  equipmentGroupInfo,
  removeGroup,
} from "@/api/equipment/equipment";
import { equipmentList } from "@/api/mock";
import data5 from "@/utils/data5";
import { listCPlotSelect } from "@/api/system/utils";

export default {
  name: "Equipment",
  data() {
    return {
      xiaoquData: [],
      xiaoquDataAdd: [],
      selectFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备分组表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plotId:null,
        lotId: null,
        groupName: null,
        time: [],
        createTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        lotId: [{ required: true, message: "小区id不能为空", trigger: "blur" }],
        groupName: [
          { required: true, message: "分组名称不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
      },
      // 查看分组数据
      groupData: {
        show: false,
        loading: false,
        row: {
          plotId: null,
          plotName: "",
          groupName: "",
          remark: "",
          id: null,
        },
        total: 0,
        pageNum: 1,
        pageSize: 10,
        list: [],
      },
    };
  },
  created() {
    listCPlotSelect({}).then((res) => {
      this.xiaoquData = res.rows;
      this.xiaoquDataAdd = res.rows;
      this.queryParams.plotId = res.rows[0].value

    }).then(res=>{
      this.getList();
    });
  },
  methods: {
    /** 查询设备分组列表 */
    getList() {
      this.loading = true;
      listEquipmentGroup(this.queryParams).then(
        (response) => {
          // equipmentList(this.queryParams).then(response => {
          this.equipmentList = response.rows;
          this.total = response.total;
          this.loading = false;
        },
        (err) => {
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lotId: null,
        groupName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备分组";
    },
    //查看
    handleView(row) {
      for (const key in this.groupData.row) {
        this.groupData.row[key] = row[key];
      }
      this.groupData.show = true;
      this.getGroupEquipmentList();
    },
    //获取分组下的设备列表
    getGroupEquipmentList() {
      this.groupData.loading = true;
      let groupId = this.groupData.row.id;
      let pageNum = this.groupData.pageNum;
      equipmentGroupInfo({ groupId, pageNum }).then(
        (res) => {
          this.groupData.loading = false;
          this.groupData.list = res.rows;
          this.groupData.total = res.total;
        },
        (err) => {
          this.groupData.loading = false;
        }
      );
    },
    //移除分组
    handleRemove(row) {
      this.$modal.confirm(`是否确认移除此设备在当前分组？`).then(() => {
        removeGroup({ equipmentId: row.id }).then((res) => {
          this.$modal.msgSuccess("移除成功");
          this.getGroupEquipmentList();
        });
      });
    },
    groupCancel() {
      this.groupData.list = [];
      this.groupData.pageNum = 1;
      this.groupData.show = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEquipmentGroup(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备分组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipmentGroup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipmentGroup(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 保存分组信息
    groupSave() {
      this.open = false;
      // this.$refs["form2"].validate(valid => {
      //   if (valid) {
      //     let objData = {
      //       id: this.groupData.row.id,
      //       plotId: this.groupData.row.plotId,
      //       groupName: this.groupData.row.groupName,
      //       remark: this.groupData.row.remark,
      //     }
      //     updateEquipmentGroup(objData).then(response => {
      //       this.$modal.msgSuccess("保存成功");
      //       this.open = false;
      //       this.getList();
      //     });
      //   }
      // });
    },
    // 批量删除
    pilinagHandleCilcik() {
      const ids = this.ids;
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag;
      } else {
        this.$modal
          .confirm("是否删除所选数据？")
          .then(function () {
            return delEquipmentGroup(ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否删除当前数据？")
        .then(function () {
          return delEquipmentGroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equipment/equipment/exportEquipmentGroup",
        {
          ...this.queryParams,
        },
        `equipment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .group-bottom-list {
  .name {
    color: #fff;
    font-size: 18px;
  }
  .pagination-container .el-pagination {
    position: relative;
  }

  .el-table .el-table__body-wrapper {
    height: auto !important;
    min-height: 200px;
  }

  .myPagination {
    display: flex;
    justify-content: center;

    .el-dialog .el-select > .el-input {
      width: 100px;
    }

    .el-pagination .el-select .el-input .el-input__inner {
      height: 28px;
    }

    .el-pagination__editor.el-input .el-input__inner {
      width: auto;
      border-radius: 4px;
    }
  }
}
$contentBg: #374056;
::v-deep .group-bottom-list {
  .el-table__header{
    tr{
      color: #ccc;
    }
  }
  .el-table .el-table__header-wrapper th, .el-table .el-table__fixed-header-wrapper th{
    color: #ccc;

  }
  .el-table{
    color: #ccc;
  }
  .el-table__cell{
    background-color: $contentBg;
  }
  .el-table tr {
    background-color: $contentBg;
    &:hover>td.el-table__cell {
          background-color: #444F64;
        }
  }

  .el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell{
    background-color: $contentBg;
  }
  .el-table__row--striped{
    &:hover>td.el-table__cell{
              background-color: #444F64 !important;
            }
  }
}
</style>
